<template>
  <div class="Full-screen-player">
    <!-- 全屏播放器 -->
    <div class="FullScreenPlayer" v-show="Fullscreen" v-if="SongDetail">
      <!-- 返回 -->
      <div @click="close" class="iconfont icon-xiangxia close"></div>

      <!-- 歌手歌名 -->
      <div class="lisinfo">
        <h2>{{ SongDetail.name }}</h2>
        <h3>
          <span v-for="(item, index) in SongDetail.ar" :key="index">{{
            item.name
          }}</span>
        </h3>
      </div>

      <!-- 背景图 -->
      <div class="imgdag">
        <img :src="SongDetail.al.picUrl" alt="" />
      </div>

      <!-- 歌曲图片 -->
      <div
        v-show="togglelyric"
        @click="togglelyric = false"
        :class="['img', 'play', { pause: iconplay }]"
        class="imgs"
        :style="'background-image: url(' + SongDetail.al.picUrl + ')'"
      ></div>

      <!-- 歌词 -->
      <div class="content" v-show="!togglelyric" @click="togglelyric = true">
        <div class="lyrics">
          <ul :style="{ top: lyricTop }">
            <li
              v-for="(item, index) in lyric"
              :key="index"
              :class="{ con: currentRow == index }"
            >
              {{ item.text }}
            </li>
          </ul>
        </div>
      </div>

      <!-- 评论按钮 -->
      <div class="comment">
        <div @click="icomfontred">
          <span v-show="icomfont" class="iconfont icon-lujing"></span>
          <span v-show="!icomfont" class="iconfont icon-lujing"></span>
        </div>
        <div class="iconfont icon-pinglun_pinglunshu" @click="comments">
          <p>999+</p>
        </div>
      </div>

      <!-- 按钮 -->
      <div class="iconfontdeg">
        <div
          @click="changeType"
          class="iconfont"
          :class="
            playerType == 1 ? 'icon-whiter-shunxubofang' : ' icon-danquxunhuan'
          "
        ></div>
        <div class="iconfont icon-shangyishou" @click="LastSong"></div>
        <div
          class="iconfont"
          :class="[iconplay ? 'icon-bofang1' : 'icon-zanting1']"
          @click="toggleplay"
        ></div>
        <div class="iconfont icon-xiayishou" @click="NextSong"></div>
        <div class="iconfont icon-xiangmu"></div>
      </div>

      <!-- 进度条 -->
      <div class="vanslider">
        <div>{{ timeLeft | playerTime }}</div>
        <div class="progressbar">
          <van-slider
            v-model="value"
            bar-height="4px"
            inactive-color="#514E4C"
            active-color="#54514e"
            button-size="12px"
            @change="sliderchange"
          />
        </div>
        <div>{{ timeRight | playerTime }}</div>
      </div>
    </div>

    <!-- 底部播放器 -->
    <div class="mini-player" v-show="!Fullscreen">
      <div :class="['img', 'play', { pause: iconplay }]" v-if="SongDetail">
        <img :src="SongDetail.al.picUrl" alt="" @click="open" />
      </div>
      <div class="Bottomplayer" v-if="SongDetail">
        <h2>{{ SongDetail.name }}</h2>
        <h3 v-if="SongDetail">
          <span v-for="(item, index) in SongDetail.ar" :key="index"
            >{{ item.name }}&nbsp;</span
          >
        </h3>
      </div>
      <div class="control" @click.stop="toggleplay">
        <span class="iconfont icon-bofang_o" v-show="iconplay"></span>
        <span class="iconfont icon-zanting_o" v-show="!iconplay"></span>
      </div>
      <div class="iconfont icon-gedan1 OpenSongIist" @click="showPopup"></div>
    </div>

    <!-- 播放器列表 -->
    <div class="Playerlist" v-show="!visible" name="PlayerlistName">
      <div class="Listcontent">
        <div class="iconfontflex">
          <h1 @click="onmouseout">
            当前播放<span>({{ playerLists.length }})</span>
          </h1>
          <div class="iconfontlist">
            <div>
              <span
                @click="changeType"
                class="iconfont"
                :class="
                  playerType == 1
                    ? 'icon-whiter-shunxubofang'
                    : ' icon-danquxunhuan'
                "
              ></span>
              {{ playerType == 1 ? "顺序播放" : "列表循环" }}
            </div>
            <div class="iconfontspan">
              <div class="iconfont icon-yunxiazai_o"></div>
              <div class="iconfont icon-ruku"></div>
              <div class="iconfont icon-lajitong" @click="DeleteAllSongs"></div>
            </div>
          </div>
        </div>
        <div class="swiper-container swiper-container-01">
          <div class="swiper-wrapper">
            <div
              class="swiper-slide item"
              v-for="(item, index) in playerLists"
              :key="index"
              @click="setId(item.id)"
            >
              <div style="text-align: center">{{ index + 1 }}</div>
              <div class="pname">{{ item.name }}</div>
              <div class="iconfont icon-shanchu" @click="delname(index)"></div>
            </div>
          </div>
          <div class="swiper-pagination swiper-pagination-01"></div>
        </div>
      </div>
    </div>
    <!-- 音频播放器 -->
    <audio
      ref="audio"
      controls
      :src="musicData"
      class="audionone"
      @loadeddata="loadeddataFun"
      @ended="endedFun"
    ></audio>
  </div>
</template>

<script>
import { getSongUrl, getSongDetail, getLyric } from "../../api/mixed";
import { getPlaylistDetail } from "../../api/ranking";
import { Toast } from "vant";
import { getArtistTopSong, getUserrecord, getLike } from "../../api/recommend";
import Swiper from "swiper";

export default {
  props: ["playerId", "listId", "getlike"],
  data() {
    return {
      visible: true,
      Fullscreen: true,
      musicData: "",
      icomfont: true,
      iconplay: true, //播放按钮
      SongDetail: null, //歌曲信息
      value: 0, //滚动条 0-100
      showPlayer: false, //全屏播放器状态
      timeRight: 0, //右侧时间
      timeLeft: 0, //左侧时间
      clearset: null, //定时器
      playerLists: [], //播放列表
      togglelyric: true, //歌词切换
      lyric: [], //歌词数组
      lyricTop: "200px", //歌词位置
      currentRow: 0, //歌词高亮
      playerType: 1, //播放類型 1.列表播放 2.随机播放
    };
  },
  created() {},
  methods: {
    close() {
      this.Fullscreen = false;
    },
    open() {
      this.Fullscreen = true;
    },
    icomfontred() {
      this.icomfont = !this.icomfont;
      if (this.icomfont == true) {
        Toast("取消收藏");
      } else {
        Toast("收藏成功");
        this.getLikeFUN();
      }
    },
    getLikeFUN() {
      getLike({ id: this.playerId }).then((data) => {
        console.log(data);
      });
    },
    // 评论
    comments() {
      this.Fullscreen = false;
      this.$router.push({
        name: "Comments",
        query: { playerId: this.playerId },
      });
    },
    // 获取歌曲url
    getSongUrlFun() {
      getSongUrl({ id: this.playerId }).then((data) => {
        this.musicData = data.data[0].url;
      });
    },
    // 获取歌曲信息
    getSongDetailFun() {
      getSongDetail({ ids: this.playerId }).then((data) => {
        this.SongDetail = data.songs[0];
      });
    },
    // 获取歌曲歌词
    getLyricFun() {
      getLyric({ id: this.playerId }).then((data) => {
        this.formatLyric(data.lrc.lyric);
      });
    },
    formatLyric(text) {
      this.lyric = [];
      let arr = text.split("\n"); //把原歌词切割成字符串
      let row = arr.length; //获取数组长度
      for (let i = 0; i < row; i++) {
        let temp_row = arr[i]; //[00:00.000] 作词 : MK王逸杰
        let temp_arr = temp_row.split("]"); //[00:00.000 , 作词 : MK王逸杰
        let lyrictext = temp_arr.pop(); //删除数组最后一个,返回值
        temp_arr.forEach((el) => {
          let obj = {};
          let time_arr = el.substr(1, el.length - 1).split(":");
          let s = time_arr[0] * 60 + Math.ceil(time_arr[1]); //把分钟变成秒
          obj.text = lyrictext;
          obj.time = s;
          if (lyrictext.length > 0) {
            this.lyric.push(obj); //每一行歌词处理后放入数组中
          }
        });
      }
    },
    // 播放暂停音乐
    toggleplay() {
      if (this.iconplay) {
        this.$refs.audio.play(); //播放音乐
        clearInterval(this.clearset); //清除定时器
        this.clearset = setInterval(() => {
          let currentTime = this.$refs.audio.currentTime; //实时变化时间
          let duration = this.$refs.audio.duration; //总时间
          this.timeLeft = currentTime;
          // 计算滚动条值
          this.value = (currentTime / duration) * 100;
          if (this.value == 100) {
            clearInterval(this.clearset);
            this.$refs.audio.pause(); //暂停音乐
            this.iconplay = true;
          }
        }, 999);
      } else {
        this.$refs.audio.pause(); //暂停音乐
        clearInterval(this.clearset);
      }
      this.iconplay = !this.iconplay; //切换页面
    },

    // 音乐播放结束后
    endedFun() {
      console.log("音乐播放结束");

      this.iconplay = true;
      // 根据播放音乐类型,选择下一首播放
      if (this.playerType == 1) {
        // 列表播放
        this.NextSong(); //下一首歌
      } else {
        // 随机播放
        let index = parseInt(Math.random() * this.playerLists.length);
        this.$emit("set-player-id", this.playerLists[index].id);
      }
    },
    // 获取滚动条变化
    sliderchange() {
      var duration = (this.$refs.audio.duration * this.value) / 100;
      this.timeLeft = duration; //修改页面数据
      this.$refs.audio.currentTime = duration; //修改播放时间
    },
    // 上一首歌
    LastSong() {
      let id = this.SongDetail.id; // 获取id
      let index = this.playerLists.findIndex((item) => item.id == id);
      index = index - 1 < 0 ? this.playerLists.length - 1 : index - 1;
      this.$emit("set-player-id", this.playerLists[index].id);
    },
    // 下一首歌
    NextSong() {
      let id = this.SongDetail.id; // 获取id
      let index = this.playerLists.findIndex((item) => item.id == id);
      index = index + 1 > this.playerLists.length - 1 ? 0 : index + 1;
      this.$emit("set-player-id", this.playerLists[index].id);
    },

    // 音乐加载完成后,获取音乐数据
    loadeddataFun() {
      this.toggleplay(); //自动播放
      let duration = this.$refs.audio.duration;
      this.timeRight = duration;
    },
    // 播放类型切换
    changeType() {
      this.playerType = this.playerType == 1 ? 2 : 1;
      if (this.playerType == 1) {
        Toast("列表播放");
      } else {
        Toast("随机播放");
      }
    },
    // 播放器列表
    showPopup() {
      this.visible = !this.visible;
    },
    setId(id) {
      getSongUrl({ id: id }).then((data) => {
        this.musicData = data.data[0].url;
      });
      getSongDetail({ ids: id }).then((data) => {
        this.SongDetail = data.songs[0];
      });
      getLyric({ id: id }).then((data) => {
        this.formatLyric(data.lrc.lyric);
      });
      this.visible = true;
    },
    onmouseout() {
      this.visible = true;
    },
    delname(index) {
      this.playerLists.splice(index, 1);
      this.NextSong();
    },
    DeleteAllSongs() {
      this.playerLists = [];
    },
  },
  watch: {
    // 获取歌曲id
    playerId() {
      this.getSongUrlFun();
      this.getSongDetailFun();
      this.getLyricFun();
    },
    // 获取歌曲列表id
    listId() {
      console.log(this.listId);
      console.log(this.$route.path);
      if (this.$route.path == "/recommend/singerID") {
        getArtistTopSong({ id: this.listId }).then((data) => {
          this.playerLists = data.songs;
        });
      } else if (this.$route.path == "/userinfo/RecentlyPlayed") {
        getUserrecord({ uid: window.localStorage.uid }).then((data) => {
          this.playerLists = data.allData;
        });
      } else {
        getPlaylistDetail({ id: this.listId }).then((data) => {
          this.playerLists = data.playlist.tracks; //获取歌单数据
        });
      }
    },
    // 歌曲已播放时间
    timeLeft() {
      for (let i = 0; i < this.lyric.length; i++) {
        if (parseInt(this.timeLeft) <= this.lyric[i].time) {
          this.lyricTop = 200 - i * 36 + "px";
          this.currentRow = i;
          break;
        }
      }
    },
    mounted() {
      new Swiper(".swiper-container-01", {
        // pagination: '.swiper-pagination-01',
        slidesPerView: 4, //显示个数
        paginationClickable: true,
        observer: true,
        observeParents: true,
        // spaceBetween: 30 //div之间间距
      });
    },
  },
  // 歌曲时间
  filters: {
    playerTime(data) {
      let m = parseInt(data / 60); //分钟
      let s = parseInt(data % 60); //秒
      if (m == 0 && s == 0) {
        return "0:00";
      } else {
        return m + ":" + (s < 10 ? "0" + s : s); // 01
      }
    },
  },
};
</script>

<style lang="less">
.Full-screen-player {
  // 全屏播放器
  .FullScreenPlayer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    color: white;
    z-index: 1;
    text-align: center;
    background-color: black;
    .iconfontdeg {
      width: 100%;
      position: fixed;
      bottom: 30px;
      left: 0px;
      color: white;
      display: flex;
      justify-content: space-around;
      div {
        font-size: 30px;
      }
    }

    .content {
      text-align: center;
      height: 64%;
      width: 100%;
      overflow: hidden;
      position: absolute;
      top: 52px;
      left: 0px;
      .lyrics {
        width: 100%;
        height: 100%;
        ul {
          width: 100%;
          height: 100%;
          position: absolute;
          transition: all 0.3s linear;
          li {
            padding-top: 2px;
            height: 36px;
            line-height: 36px;
            color: #cbcccc;
            font-size: 16px;
            overflow: hidden;
            &.con {
              color: #fff;
            }
          }
        }
      }
    }
    .close {
      font-size: 30px;
      width: 30px;
      height: 30px;
      line-height: 30px;
      padding: 10px;
      position: fixed;
      text-align: center;
      z-index: 1;
    }
    .imgs {
      border: 12px solid #8d8987;
      width: 69%;
      height: 39%;
      background-size: 100%;
      background-repeat: no-repeat;
      border-radius: 50%;
      transform-origin: center center;
      position: fixed;
      top: 20%;
      left: 13%;
      &.play {
        animation: rotate 20s linear infinite;
      }
      &.pause {
        animation-play-state: paused;
      }
    }
    .imgdag {
      width: 100%;
      height: 100%;
      img {
        filter: blur(18px);
        position: absolute;
        bottom: -100px;
        transform: scale(2.8);
        z-index: -1;
      }
    }
    .lisinfo {
      display: block;
      h2 {
        padding-top: 8px;
        font-size: 16px;
      }
      h3 {
        padding: 3px 0;
        font-size: 12px;
      }
    }
    .vanslider {
      width: 100%;
      position: fixed;
      bottom: 13%;
      display: flex;
      justify-content: space-around;
      .progressbar {
        padding-top: 6px;
        width: 60%;
      }
    }
    .comment {
      width: 60%;
      position: fixed;
      bottom: 20%;
      left: 20%;
      display: flex;
      justify-content: space-around;
      div {
        width: 20px;
        height: 20px;
        font-size: 30px;
        position: relative;
        span {
          font-size: 30px;
          position: absolute;
          &:nth-child(2) {
            color: red;
          }
        }
        p {
          position: absolute;
          top: -3px;
          right: -30px;
          font-size: 8px;
        }
      }
    }
  }

  // 底部播放器
  .mini-player {
    position: fixed;
    left: 0px;
    bottom: 0px;
    width: 100%;
    height: 40px;
    background-color: #fbfbfb;
    opacity: 0.9;
    display: flex;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    .img {
      height: 40px;
      width: 40px;
      margin: 0px 10px 0px 15px;
      background-color: aqua;
      border-radius: 50%;
      img {
        height: 40px;
        width: 40px;
        border-radius: 50%;
      }
      &.play {
        animation: rotate 20s linear infinite;
      }
      &.pause {
        animation-play-state: paused;
      }
    }
    .Bottomplayer {
      flex: 1;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      h2 {
        font-size: 16px;
        line-height: 16px;
        color: #131314;
        margin-top: 9px;
      }
      h3 {
        line-height: 12px;
        font-size: 12px;
        color: #929393;
      }
    }
    .control {
      text-align: center;
      width: 26px;
      height: 26px;
      line-height: 26px;
      margin: 5px 30px 0 0;
      .iconfont {
        color: #464848;
        font-size: 40px;
      }
    }
    .OpenSongIist {
      width: 30px;
      height: 30px;
      padding-right: 20px;
      color: #464848;
      font-size: 35px;
      text-align: center;
    }
  }

  @keyframes rotate {
    0% {
      transform: rotate(0);
    }
    100% {
      transform: rotate(360deg);
    }
  }
  .Playerlist {
    width: 100%;
    height: 55vh;
    background-color: white;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    position: fixed;

    bottom: 0px;
    left: 0px;
    .Listcontent {
      height: 100vh;
      .iconfontflex {
        h1 {
          font-size: 16px;
          padding: 24px 16px 10px 16px;
          span {
            font-size: 15px;
            color: #a7a7a7;
          }
        }
        .iconfontlist {
          width: 95%;
          margin: 0px auto;
          display: flex;
          align-items: flex-end;
          justify-content: space-between;
          div {
            font-size: 20px;
            line-height: 24px;
            font-weight: 500;
            span {
              font-size: 30px;
            }
          }
          .iconfontspan {
            width: 30%;
            display: flex;
            justify-content: space-between;
            div {
              font-size: 24px;
            }
          }
        }
      }
      .swiper-container {
        .swiper-wrapper {
          .swiper-slide {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin: 10px 0;
            div {
              width: 30px;
            }
            .pname {
              flex: 1;
              padding: 0px 0px;
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  .audionone {
    display: none;
  }
}
</style>